package com.shamine.kuaiqiao.blink.gallery

import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.datatype.BmobFile
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.UpdateListener
import cn.bmob.v3.listener.UploadFileListener
import com.shamine.kuaiqiao.blink.data.Gallery
import com.shamine.kuaiqiao.blink.data.Photo
import com.shamine.kuaiqiao.blink.event.EventUserGalleries
import com.shamine.kuaiqiao.blink.data.Passer
import com.shamine.kuaiqiao.blink.events.EventPhotoUpload
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import java.io.File

class Worker(context: GalleryActivity) {
    private lateinit var query: BmobQuery<Gallery>

    fun getGalleries() {
        query = BmobQuery()
        query.order("createdAt")
        query.setLimit(50)
        query.addWhereEqualTo("deleted", false)
        query.include("passer")
        query.addWhereEqualTo("passer", BmobUser.getCurrentUser())
        query.findObjectsObservable(Gallery::class.java).subscribe(object : Subscriber<List<Gallery>>() {
            override fun onError(throwable: Throwable?) {
                EventBus.getDefault().post(EventUserGalleries(false, throwable?.message.toString()))
                if (isUnsubscribed) unsubscribe()
            }

            override fun onCompleted() {
                if (isUnsubscribed) unsubscribe()
            }

            override fun onNext(list: List<Gallery>?) {
                EventBus.getDefault().post(EventUserGalleries(true, list!!.toMutableList()))
            }

        })
    }

    /**
     * 上传照片
     */
    fun upload(path: String?, gallery: Gallery) {
        val file = BmobFile(File(path))
        //1.上传照片
        file.upload(object : UploadFileListener() {
            override fun done(e: BmobException?) {
                if (e == null) {
                    //2.关联到相册
                    val photo = Photo(gallery, BmobUser.getCurrentUser(Passer::class.java), file)
                    photo.saveObservable().subscribe(object : Subscriber<String>() {
                        override fun onCompleted() {
                        }

                        override fun onError(throwable: Throwable) {
                            EventBus.getDefault().post(EventPhotoUpload(false, throwable.message.toString()))
                        }

                        override fun onNext(objectId: String) {
                            val tempGallery = Gallery(gallery.size + 1, false)
                            tempGallery.update(gallery.objectId, object : UpdateListener() {
                                override fun done(p0: BmobException?) {
                                    if (p0 == null) {
                                        gallery.size = tempGallery.size
                                        EventBus.getDefault().post(EventPhotoUpload(true, photo, gallery.size))
                                    } else EventBus.getDefault().post(EventPhotoUpload(false, p0.message.toString()))
                                }
                            })
                        }
                    })
                } else {
                    EventBus.getDefault().post(EventPhotoUpload(false, e.message.toString()))
                }
            }
        })
    }
}